﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using JobBoardAdminPanel.Commands;
using JobBoardAdminPanel.Helpers;
using JobsBoardDAL.DTOs;

namespace JobBoardAdminPanel.ViewModels
{
    public class EditTownsViewModel : ViewModelBase
    {
        private ObservableCollection<CityViewModel> cities;
        private CountryViewModel currentCountry;
        private ObservableCollection<CountryViewModel> countries;
        private CityViewModel currentCity;
        private AddCityCommand addCommand;
        private DeleteCityCommand deleteCommand;
        private SaveCityCommand saveCommand;
        private CityViewModel newCity;


        public EditTownsViewModel()
        {
            countries = new ObservableCollection<CountryViewModel>(ViewModelsHelper.GetAllCountries());
            this.addCommand = new AddCityCommand(this);
            this.deleteCommand = new DeleteCityCommand(this);
            this.saveCommand = new SaveCityCommand(this);
            newCity = new CityViewModel(new CityDTO());
        }
  
        public CityViewModel NewCity
        {
            get
            {
                return newCity;
            }
            set
            {
                if (this.newCity != value)
                {
                    newCity = value;
                    OnPropertyChanged("NewCity");
                }
            }
        }

        public AddCityCommand AddCommand
        {
            get
            {
                return addCommand;
            }
            set
            {
                addCommand = value;
            }
        }

        public DeleteCityCommand DeleteCommand
        {
            get
            {
                return deleteCommand;
            }
            set
            {
                deleteCommand = value;
            }
        }

        public SaveCityCommand SaveCommand
        {
            get
            {
                return saveCommand;
            }
            set
            {
                saveCommand = value;
            }
        }

        public CityViewModel CurrentCity
        {
            get
            {
                return currentCity;
            }
            set
            {
                if (this.currentCity != value)
                {
                    currentCity = value;
                    OnPropertyChanged("CurrentCity");
                }
            }
        }

        public ObservableCollection<CityViewModel> Cities
        {
            get
            {
                return cities;
            }
            set
            {
                if (this.cities != value)
                {
                    cities = value;
                    OnPropertyChanged("Cities");
                }
            }
        }

        public void RefreshCities()
        {
            this.Cities = new ObservableCollection<CityViewModel>(ViewModelsHelper.GetCitiesByCountry(CurrentCountry));
        }

        public CountryViewModel CurrentCountry
        {
            get
            {
                return currentCountry;
            }
            set
            {
                if (this.CurrentCountry != value)
                {
                    currentCountry = value;
                    RefreshCities();
                    OnPropertyChanged("CurrentCountry");
                }
            }
        }

        public ObservableCollection<CountryViewModel> Countries
        {
            get
            {
                return countries;
            }
            set
            {
                if (this.countries != value)
                {
                    countries = value;
                    OnPropertyChanged("Countries");
                }
            }
        }

        internal void Update()
        {
            if (this.CurrentCity != null)
            {
                this.CurrentCity.Update();
                RefreshCities();
                this.CurrentCity = null;
            }
        }

        internal void Delete()
        {
            if (this.CurrentCity != null)
            {
                this.CurrentCity.Delete();
                RefreshCities();
            }
        }

        internal void Add()
        {
            this.NewCity.Add(this.CurrentCountry.CountryName);
            RefreshCities();
        }
    }
}